package com.piece.core.mongo.service;

import com.piece.core.mongo.repository.MongoStreamBlockRepository;
import com.piece.core.mongo.entity.MongoStreamBlock;
import com.piece.core.framework.support.bootstrap.DataTable;
import com.piece.core.framework.support.bootstrap.QueryTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service(value = "mongoStreamBlockService")
public class MongoStreamBlockService {

    @Autowired
    private MongoStreamBlockRepository mongoStreamBlockRepository;

    public MongoStreamBlock insert(MongoStreamBlock visitor) {
        return mongoStreamBlockRepository.save(visitor);
    }

    public MongoStreamBlock update(MongoStreamBlock visitor) {
        return mongoStreamBlockRepository.saveOrUpdate(visitor.getId(), visitor);
    }

    public void delete(String id) {
        MongoStreamBlock visitor = new MongoStreamBlock();
        visitor.setId(id);
        mongoStreamBlockRepository.delete(visitor);
    }

    /**
     * 根据ip删除数据
     */
    public void deleteByIp(String ip) {
        MongoStreamBlock query = new MongoStreamBlock();
        query.setIp(ip);
        mongoStreamBlockRepository.deleteByCondition(query);
    }

    public void clear() {
        mongoStreamBlockRepository.deleteAll();
    }

    public DataTable findPage(QueryTable queryTable) {
        return mongoStreamBlockRepository.findPage(queryTable);
    }

    public List<MongoStreamBlock> findByIp(String ip) {
        Map<String, Object> params = new HashMap<>();
        params.put("Q_ip_S_EQ", ip);
        return mongoStreamBlockRepository.findByCondition(params);
    }
}